package gitobject;
import repository.Repository;
import zlib.ZLibUtils;

import java.io.*;


public class GitObject implements Serializable{
    public static long serialVersionUID = 4754631534532L;

    protected String fmt;                  //type of object
    protected String key;                  //key of object
    protected String mode;                 //mode of object
    protected static String path;          //absolute path of objects

    static {
        try {
            path = new File(".").getCanonicalPath() + File.separator+ ".jit" + File.separator + "objects";
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected String value;                //value of object

    public String getFmt(){
        return fmt;
    }
    public String getKey() { return key; }
    public void setKey(String key) {this.key = key;};
    public String getMode(){
        return mode;
    }
    public String getPath() {
        return path;
    }
    public String getValue(){
        return value;
    }
    public void setValue(String value){
        this.value = value;
    }
    public GitObject(){}
    /**
     * Get the value(content) of file
     * @param file
     * @return String
     * @throws IOException
     */
    public static String getValue(File file) throws IOException {
        FileInputStream is = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        String s = new String();
        while((len = is.read(buffer)) != -1){
            String str = new String(buffer, 0, len);
            s += str;
        }
        if(is != null){
            is.close();
        }
        return s;
    }
    /**
     * Todo: Serialize the object to the local repository.
     * @throws Exception
     */
    public void writeObject(String temPath){
        ObjectOutputStream oos = null;
        try{
            oos = new ObjectOutputStream(new FileOutputStream(temPath));
            oos.writeObject(this);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(oos != null){
                try {
                    oos.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Todo: Serialize the object and compress with zlib.
     * @throws Exception
     */
    public void compressWrite(String temPath){
        ByteArrayOutputStream os = null;
        ObjectOutputStream oos = null;
        FileOutputStream f = null;
        try{
            os = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(os);
            f = new FileOutputStream(temPath);
            oos.writeObject(this);
            byte[] inputData = os.toByteArray();
            byte[] data = ZLibUtils.compress(inputData);
            ZLibUtils.compress(data, f);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(oos != null){
                try {
                    oos.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
